import { showSuccessToast, showFailToast, showConfirmDialog, showDialog } from 'vant';


export default {
    install(app)
    {
        // 从pinia中获取数据，并将个人资料挂载到全局
        let ShowStore = app.config.globalProperties.$ShopStore
        let {business, cart, address} = storeToRefs(ShowStore)

        //检查是否登录的方法
        const check = async () =>
        {
            let id = business.value.hasOwnProperty('id') ? business.value.id : 0
            let mobile = business.value.hasOwnProperty('mobile') ? business.value.mobile : ''

            //调用接口查询
            var result = await app.config.globalProperties.$POST({
                url: '/business/check',
                params: {id, mobile}
            })

            if(result.code == 1)
            {
                business.value = result.data

                //修改一下全局数据
                app.config.globalProperties.$business = business
            }else
            {
                //登录失败
                business.value = {}
                app.config.globalProperties.$business = business
            }
            
            return result
        }


        // app == vue应用 globalProperties 全局属性.自定义属性
        //在全局应用上面添加了一个自定义的属性
        app.config.globalProperties.$success = showSuccessToast
        app.config.globalProperties.$fail = showFailToast
        app.config.globalProperties.$confirm = showConfirmDialog
        app.config.globalProperties.$alert = showDialog
        app.config.globalProperties.$check = check
        
        app.config.globalProperties.$business = business
        app.config.globalProperties.$cart = cart  //立即购买的商品数据
        app.config.globalProperties.$address = address //确认订单收货地址
    }
}